package com.geeka.d12.tcpmulti;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServerMulti {
    public static void main(String[] args) {
        /**
         * 接受多个客户端的连接
         */

        int port = 9999;

        try (ServerSocket serverSocket = new ServerSocket(port);) {
            System.out.println("server启动:" + port);
            while (true) {
                // 阻塞等待client的连接
                Socket socket = serverSocket.accept();
                String cname = socket.getInetAddress() + " " + socket.getPort();
                System.out.println("有人连接上了 " + cname);
                ServerThread serverThread = new ServerThread(socket);
                serverThread.start();


//                String cname = socket.getInetAddress() + " " + socket.getPort();
//                System.out.println("有人连接上了 " + cname);
//                // 读取客户端的信息
//                InputStream ips = socket.getInputStream();
//                BufferedReader br = new BufferedReader(new InputStreamReader(ips));
//                String str = null;
//                while ((str = br.readLine()) != null) {
//                    System.out.println(cname + "说: " + str);
//                }
//                System.out.println("server 执行到这里");
            }

        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

    }
}
